<?php
/*
	本插件：$this->model('model')->方法名();
	其他地方调用：model('jy_newganfee/model')->方法名();
*/
namespace jy_newganfee;
defined('BY_JYA') or exit('error');
class model extends \plugin{
	public function current_level_up_types(){
		return str_array($this->fenxiaoset['level_up_types']);
	}
	/*
		日志信息
	*/
	public function set_log($params,$type=0){
		$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$params['mid']?:$_SESSION['uid'],
			'pingid'=>$params['pingid'],
			'msg'=>$params['msg'],
			'status'=>$params['status'],
			'gift_type'=>$params['gift_type'],
			'other_goodsid'=>$params['other_goodsid'],
			'other_credit_fee'=>$params['other_credit_fee'],
			'type'=>$type,
			'timeid'=>$params['timeid'],
			'dayid'=>date('Y-m-d',TIMESTAMP),
			'teamid'=>$params['teamid'],
			'canzhong'=>$params['canzhong'],
			'createtime'=>TIMESTAMP
		);
		pdo_insert('jy_newganfee_log',$insert);
		return true;
	}
	/*
		fee信息0自己兑换余额，1推荐下级兑换余额
	*/
	public function set_feelog($params,$type=0){
		$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$params['mid']?:$_SESSION['uid'],
			'msg'=>$params['msg'],
			'status'=>$params['status'],
			'gift_type'=>$params['gift_type'],
			'fee'=>$params['fee'],
			'type'=>$type,
			'dayid'=>date('Y-m-d',TIMESTAMP),
			'createtime'=>TIMESTAMP
		);
		pdo_insert('jy_newganfee_feelog',$insert);
		return true;
	}
	/*
		推荐N个人参与了
	*/
	public function get_agentnum($mid=0){
		$mid=$mid?:$_SESSION['uid'];
		$downmids=pdo_getall('jy_newganfee_member',array('fromuser'=>$mid,'uniacid'=>$_SESSION['uniacid']),'mid','mid');
		$canyusnum=0;
		if($downmids){
			$downmids=array_keys($downmids);
			$canyus=pdo_getnorepeat('jy_newganfee_log',array('status'=>1,'type'=>0,'mid' =>$downmids,'uniacid'=>$_SESSION['uniacid']),'mid');
			$canyusnum=count($canyus);
		}
		return $canyusnum;
	}
	public function add_upfee(){
		if($this->fenxiaoset['yuered_open']!=1){
			exi('分销设置--分红--未《开启月度加权分红》！','error');
		}
		$end_time=strtotime(date("Y-m",time()));
		//$end_time=time();
		$start_time = strtotime(date('Y-m',$end_time-10*24*3600));
		$month=date("Y-m",$start_time+200);
		$params=array('uniacid'=>$_SESSION['uniacid'],'month'=>$month);
		$upfeelog=$this->get('upfeelog',$params);
		if($upfeelog['id']){
			exi('上月的分红已经派发！','error');
		}
		$params=array('plugin_name'=>'jy_newganfee','uniacid'=>$_SESSION['uniacid'],'createtime between'=>array($start_time,$end_time));
		$allfee=pdo_getsum('jy_weishop_order',$params,'price');
		$upfeelog=array(
			'totalfee'=>$allfee,
			'month'=>$month,
		);
		$this->set_upfeelog($upfeelog);
		$level_ids=$this->getall('level',array('hehuo_rate >'=>0,'uniacid'=>$_SESSION['uniacid']),array('id','hehuo_rate'));
		$alluser_num=0;
		$allmoney=0;
		foreach($level_ids as $lv){
			$fenfee=$allfee*$lv['hehuo_rate']/100;
			$members=$this->getall('member',array('uniacid'=>$_SESSION['uniacid'],'level_id'=>$lv['id']));
			$count=count($members);
			if($count<=0){
				continue;
			}
			$money=$fenfee/$count;
			if($money<=0){
				continue;
			}
			foreach($members as $item){
				$allmoney +=$money;
				$alluser_num ++;
				$insert=array(
					'uniacid'=>$_SESSION['uniacid'],
					'mid'=>$item['mid'],
					'money'=>$money,
					'createtime'=>TIMESTAMP,
					'status'=>0,
					'type'=>7,
					'level'=>1,
				);
				$this->pdo_insert('commission',$insert);
			}
		}
		$this->update('upfeelog',array('user_num'=>$alluser_num,'money'=>$allmoney),array('uniacid'=>$_SESSION['uniacid'],'month'=>$month));
		return true;
	}
	public function add_dayredfee(){
		if($this->fenxiaoset['dayred_open']!=1 || !$this->fenxiaoset['dayred_rate']){
			return false;
			exi('分销设置--分红--未《开启月度加权分红》！','error');
		}
		$end_time=strtotime(date("Y-m-d",time()));
		$start_time = $end_time-24*3600;
		$month=date("Y-m-d",$start_time+200);
		$params=array('uniacid'=>$_SESSION['uniacid'],'type'=>1,'month'=>$month);
		$upfeelog=$this->get('upfeelog',$params);
		if($upfeelog['id']){
			//再获取前天的
			$end_time=$start_time;
			$start_time = $end_time-24*3600;
			$month=date("Y-m-d",$start_time+200);
			$params=array('uniacid'=>$_SESSION['uniacid'],'type'=>1,'month'=>$month);
			$upfeelog=$this->get('upfeelog',$params);
			if($upfeelog['id']){
				return false;
			}
		}
		$params=array('uniacid'=>$_SESSION['uniacid'],'createtime between'=>array($start_time,$end_time));
		$allfee=$this->getsum('neworder',$params,'price');
		$upfeelog=array(
			'totalfee'=>$allfee,
			'month'=>$month,
		);
		$this->set_upfeelog($upfeelog,1);
			$alluser_num=0;
			$allmoney=0;
			$fenfee=$allfee*$this->fenxiaoset['dayred_rate']/100;
			$members=$this->getall('member',array('uniacid'=>$_SESSION['uniacid'],'level_id >'=>0));
			$count=count($members);
			if($count<=0){
				return false;
			}
			$money=$fenfee/$count;
			if($money<=0){
				return false;
			}
			foreach($members as $item){
				$isgetfee=$this->getsum('commission',array('mid'=>$item['mid'],'uniacid'=>$_SESSION['uniacid'],'type'=>13),'money');
				if($this->fenxiaoset['dayred_man'] && $this->fenxiaoset['dayred_man']<=$isgetfee){
					continue;
				}
				$allmoney +=$money;
				$alluser_num ++;
				$insert=array(
					'uniacid'=>$_SESSION['uniacid'],
					'mid'=>$item['mid'],
					'money'=>$money,
					'createtime'=>TIMESTAMP,
					'status'=>1,
					'type'=>13,
					'level'=>1,
				);
				$this->pdo_insert('commission',$insert);
			}
			$this->update('upfeelog',array('user_num'=>$alluser_num,'money'=>$allmoney),array('uniacid'=>$_SESSION['uniacid'],'month'=>$month));
			return true;
	}
	/*
		生成分红期号
	*/
	public function set_upfeelog($params,$type=0){
		$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'user_num'=>$params['user_num'],
			'totalfee'=>$params['totalfee'],
			'money'=>$params['money'],
			'month'=>$params['month'],
			'type'=>$type,
			'createtime'=>TIMESTAMP
		);
		$this->pdo_insert('upfeelog',$insert);
		return true;
	}
	/*
		首单记录
	*/
	public function set_neworder($order,$goods=array()){
		$params=array('uniacid'=>$_SESSION['uniacid'],'mid'=>$order['mid']);
		$isget=$this->get('neworder',$params);
		if($isget['id']){
			return false;
		}
		$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$order['mid'],
			'orderid'=>$order['id'],
			'goodsid'=>$goods['goodsid'],
			'price'=>$order['price'],
			'month'=>date("Y-m-d",TIMESTAMP),
			'type'=>0,
			'createtime'=>TIMESTAMP
		);
		$this->pdo_insert('neworder',$insert);
		return true;
	}
	/*
		收入分配
	*/
	public function up_fee($money,$mid=0){
		$fu_rate=$this->fenxiaoset['fu_rate'];
		$fu_feetype=$this->fenxiaoset['fu_feetype'];
		if($mid && $fu_feetype && $fu_rate>0 && $money>0){
			$fufee=$fu_rate*$money/100;
			$this->set_fu_fee($money,$mid);
		}else{
			$fufee=0;
		}
		return $fufee;
	}
	/*
		发放10%到复消币
	*/
	public function set_fu_fee($money,$mid=0){
		$fu_rate=$this->fenxiaoset['fu_rate'];
		$fu_feetype=$this->fenxiaoset['fu_feetype'];
		$money=$fu_rate*$money/100;
		if($mid && $fu_feetype && $money>0){
			model('core/member')->member_credit($mid,'credit'.$fu_feetype,$money,true,'jy_newganfee','收入拿出%用做复消');
			$this->set_fu_man($mid);
		}
		return true;
	}
	/*
		满N元自动复消产品
	*/
	public function set_fu_man($mid=0){
		$fu_feetype=$this->fenxiaoset['fu_feetype'];
		$fu_man=$this->fenxiaoset['fu_man'];
		if($mid && $fu_man>0 && $fu_feetype){
			$membinfo=pdo_get('core_members',array('id'=>$mid),array('credit'.$fu_feetype));
			if($membinfo['credit'.$fu_feetype]>=$fu_man){
				$this->model('order')->create($mid,$fu_man);
				model('core/member')->member_credit($mid,'credit'.$fu_feetype,$fu_man,false,'jy_newganfee','满N元自动复消产品');
			}
		}
		return true;
	}
	/*
		团队人数
	*/
	public function get_downnum($mid=0,$islevel=false,$level_id=0){
		$downmids=$mid;
		$numarray=array();
		for($i=0;$i<6;$i++){
			$params=array('fromuser'=>$downmids,'uniacid'=>$_SESSION['uniacid']);
			if($islevel){
				$params['level_id']=$level_id;
			}
			$downmids=pdo_getall('jy_newganfee_member',$params,'mid','mid');
			$numarray=array_merge($numarray,$downmids);
			if($downmids){
				$downmids=array_keys($downmids);
			}else{
				break;
			}		
		}
		$num=count($numarray);
		return $num;
	}
}
?>